using Xunit;

namespace AutoRegisterInject.Tests;

public partial class GenerationTests
{
    [Fact]
    public async Task ShouldRegisterHosted()
    {
        const string INPUT = @"
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
[RegisterHostedService]
public class Foo : IHostedService 
{
    public Task StartAsync(CancellationToken stoppingToken) => throw new System.NotImplementedException();
    public Task StopAsync(CancellationToken stoppingToken) => throw new System.NotImplementedException();
}";

        const string EXPECTED = @"// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        serviceCollection.AddHostedService<Foo>();
        return serviceCollection;
    }
}";

        await RunGenerator(INPUT, EXPECTED);
    }

    [Fact]
    public async Task ShouldRegisterHostedWithInterfaceAndIgnoreIt()
    {
        const string INPUT = @"
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
[RegisterHostedService]
public class Foo : IHostedService, IFoo
{
    public Task StartAsync(CancellationToken stoppingToken) => throw new System.NotImplementedException();
    public Task StopAsync(CancellationToken stoppingToken) => throw new System.NotImplementedException();
}
public interface IFoo { }
";

        const string EXPECTED = @"// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        serviceCollection.AddHostedService<Foo>();
        return serviceCollection;
    }
}";

        await RunGenerator(INPUT, EXPECTED);
    }

    [Fact]
    public async Task ShouldRegisterHostedWithBaseClass()
    {
        const string INPUT = @"
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
[RegisterHostedService]
public class Foo : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken) => throw new System.NotImplementedException();
}
";

        const string EXPECTED = @"// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        serviceCollection.AddHostedService<Foo>();
        return serviceCollection;
    }
}";

        await RunGenerator(INPUT, EXPECTED);
    }
}